package my;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculater extends JFrame {
    private JPanel contentPane;
    private JTextField tfWidth;
    private JTextField tfThinkness;
    private JButton btn1;
    private JButton btn2;
    private JTextField tfCurrent1, tfJc1;
    private JTextField tfCurrent2, tfJc2;

    public Calculater() {
        setTitle("计算HallBar电流密度");
        setBounds(100, 100, 600, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridLayout(3, 1, 10, 10));
        setContentPane(contentPane);

        /*Hall Bar尺寸界面*/
        //创建控件
        JLabel lblWidth = new JLabel("宽度(um)");
        JLabel lblThinkness = new JLabel("厚度(nm)");
        tfWidth = new JTextField("50");
        tfWidth.setColumns(10);
        tfThinkness = new JTextField("40");
        tfThinkness.setColumns(10);
        //在面板重添加控件
        JPanel msgPanel = new JPanel();
        msgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        msgPanel.setBorder(new TitledBorder(null, "Hall Bar尺寸"));
        msgPanel.add(lblWidth);
        msgPanel.add(tfWidth);
        msgPanel.add(lblThinkness);
        msgPanel.add(tfThinkness);
        contentPane.add(msgPanel);

        /*由尺寸和电流计算电流密度*/
        //创建控件
        JLabel lblCurrent1 = new JLabel("电流(mA)");
        JLabel lblJc1 = new JLabel("电流密度(A/cm^2)");
        tfCurrent1 = new JTextField();
        tfCurrent1.setColumns(10);
        tfJc1 = new JTextField();
        tfJc1.setColumns(10);
        btn1 = new JButton("计算");
        //在面板重添加控件
        JPanel upPanel = new JPanel();
        upPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        upPanel.setBorder(new TitledBorder(null, "由尺寸和电流计算电流密度"));
        upPanel.add(lblCurrent1);
        upPanel.add(tfCurrent1);
        upPanel.add(btn1);
        upPanel.add(lblJc1);
        upPanel.add(tfJc1);
        contentPane.add(upPanel);

        /*由尺寸和电流密度计算电流界面*/
        //创建控件
        JLabel lblCurrent2 = new JLabel("电流(mA)");
        JLabel lblJc2 = new JLabel("电流密度(A/cm^2)");
        tfCurrent2 = new JTextField();
        tfCurrent2.setColumns(10);
        tfJc2 = new JTextField();
        tfJc2.setColumns(10);
        btn2 = new JButton("计算");
        //在面板重添加控件
        JPanel downPanel = new JPanel();
        downPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        downPanel.setBorder(new TitledBorder(null, "由尺寸和电流密度计算电流"));
        downPanel.add(lblJc2);
        downPanel.add(tfJc2);
        downPanel.add(btn2);
        downPanel.add(lblCurrent2);
        downPanel.add(tfCurrent2);
        contentPane.add(downPanel);

        addActionListener();
    }

    private void addActionListener() {
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double width = 1e-4 * Double.parseDouble(tfWidth.getText());
                    double thinkness = 1e-7 * Double.parseDouble(tfThinkness.getText());
                    double current = 1e-3 * Double.parseDouble(tfCurrent1.getText());
                    double Jc = current / (width * thinkness);
                    tfJc1.setText(String.format("%.4g", Jc) + "");
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(Calculater.this, "请输入正确的数字！", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double width = 1e-4 * Double.parseDouble(tfWidth.getText());
                    double thinkness = 1e-7 * Double.parseDouble(tfThinkness.getText());
                    double Jc = Double.parseDouble(tfJc2.getText());
                    double current = 1e3 * Jc * (width * thinkness);
                    tfCurrent2.setText(String.format("%.4g", current) + "");
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(Calculater.this, "请输入正确的数字！", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }

    public static void main(String[] args) {
        new Calculater().setVisible(true);
    }
}
